package com.ky.facescan.utils;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;

import androidx.core.content.FileProvider;

import com.ky.facescan.BuildConfig;
import com.ky.facescan.R;
import com.tbruyelle.rxpermissions.RxPermissions;

import java.io.File;
import java.lang.reflect.Method;
import java.util.UUID;

import rx.functions.Action1;


public class DeviceUtils {


    public static String getDeviceSN() {
        String serial = null;
        try {
            Class<?> c = Class.forName("android.os.SystemProperties");
            Method get = c.getMethod("get", String.class);
            serial = (String) get.invoke(c, "ro.serialno");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return serial;
    }


    public static void requestPermission(Context context, final PermissionCall call, final String... permissions) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            // 大于等于23，去申请权限
            RxPermissions.getInstance(context.getApplicationContext())
                    //多个权限用","隔开
                    .request(permissions)
                    .subscribe(new Action1<Boolean>() {
                        @Override
                        public void call(Boolean aBoolean) {
                            if (aBoolean) {
                                call.granted();
                            } else {
                                call.denied();
                            }
                        }
                    });
        } else {
            call.granted();
        }
    }

    public interface PermissionCall {
        //授予
        void granted();

        //拒绝
        void denied();
    }

    public static void installApk(Context context, File file) {
        //判断是否需要兼容7.0
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

            LogUtils.e("apkFile:" + file.getPath());
            Uri apkUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", file);
            Intent installIntent = new Intent(Intent.ACTION_VIEW);
            installIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            // 给目标应用一个临时授权
            installIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            installIntent.setDataAndType(apkUri, "application/vnd.android.package-archive");
            context.startActivity(installIntent);
        } else {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.parse(file.getPath()), context.getString(R.string.action_install));
            context.startActivity(intent);
        }
    }

    public static String getVersionName(Context context) {
        // 获取packagemanager的实例
        PackageManager packageManager = context.getPackageManager();
        // getPackageName()是你当前类的包名，0代表是获取版本信息
        PackageInfo packInfo = null;
        try {
            packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        String versionName = packInfo.versionName;
//        int versionCode = packInfo.versionCode;
        return versionName;
    }
}
